package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;
import com.itany.shop.util.PageInfo;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @Author:石小俊
 * @Date:2022/11/15 16:14
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
public class UserController {

    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        new UserController().showMenu();
    }

    /**
     * 显示菜单
     */
    public void showMenu(){
        System.out.println("----------------------------------------");
        System.out.println("欢迎来到网络商城购物系统");
        System.out.println("----------------------------------------");
        System.out.println("1.注册\t\t2.登录\t\t3.显示商品列表");
        System.out.println("----------------------------------------");
        System.out.print("请选择:");
        int selected = sc.nextInt();
        switch (selected){
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                showProduct();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入");
                break;
        }
    }

    public void register(){
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入电话:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();

        try {
            userService.register(username,password,phone,address);
            System.out.println("恭喜您,注册成功");
        } catch (RequestParameterErrorException e) {
            System.out.println("对不起,注册失败,失败原因:"+e.getMessage());
        } catch (UserExistException e) {
            System.out.println("对不起,注册失败,失败原因:"+e.getMessage());
        }
        // 回到菜单
        showMenu();
    }

    public void login(){
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        try {
            User user = userService.login(username,password);
            System.out.println("登录成功,欢迎您:"+user.getUsername());
//            MainController mc = new MainController(user);
//            mc.showMenu();
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("对不起,登录失败,失败原因:"+e.getMessage());
        } catch (UserNotExistException e) {
            System.out.println("对不起,登录失败,失败原因:"+e.getMessage());
        }
    }

    public void showProduct(){
//        List<Product> products = productService.findAll();
        System.out.print("请输入页码:");
        Integer pageNo = sc.nextInt();
        System.out.print("请输入一页多少条:");
        Integer pageSize = sc.nextInt();
//        List<Product> products = productService.findPage(pageNo,pageSize);
//        System.out.println("-------------------------------------------");
//        System.out.println("编号\t\t商品名称\t\t商品单价");
//        for(Product product : products){
//            System.out.println(product.getId()+"\t\t"+product.getName()+"\t\t"+product.getPrice());
//        }

        PageInfo<Product> productPage = productService.findPageInfo(pageNo,pageSize);
        System.out.println("-------------------------------------------");
        System.out.println("编号\t\t商品名称\t\t商品单价");
        for(Product product : productPage.getList()){
            System.out.println(product.getId()+"\t\t"+product.getName()+"\t\t"+product.getPrice());
        }
        System.out.println("-------------------------------------------");
        System.out.print("上一页");
        for(int i = 1; i <= productPage.getPages(); i++){
            System.out.print(" "+i+" ");
        }
        System.out.print("下一页");



    }

}
